define(['jquery','jquery_route', 'pageJs','config', 'arttemplateUtil','common','msg_util','validform','wdatePicker',,'bootstrap_typeahead'], function($,jquery_route,pageJs,config, arttemplateUtil,common,msg_util) {
	$.controller("orderController", function($model, $tplHtml, $defobj,
			$modelobj, $paramobj, $this) {
		$model($defobj);
		var param = {order_tab:1};
		//传入页签
		if($paramobj.order_tab != undefined && $paramobj.order_tab != 'undefined'){
			$('#tabs-632335 a[tab_value="'+$paramobj.order_tab+'"]').tab('show')
			param.order_tab=$paramobj.order_tab;
		}
		if($paramobj.order_id != undefined && $paramobj.order_id != 'undefined'){
			$('#order_id').val($paramobj.order_id)
		}
		if($paramobj.user_account != undefined && $paramobj.user_account != 'undefined'){
			$('#user_account').val($paramobj.user_account)
		}
		var initData = function(){
			common.loading();
			common.common_ajax_request('/api/seller/v1/queryOrderList',param,true,function(data,code,msg){
				if(msg.businessCode==0){
					initStatistics(data);
					setData(data);
					pageJs.setPage(data.pageNo,data.totalPage,data.totalCount,callback);
				}else{
					setData({});
					window.layer.msg("系统异常");
				}
				layer.closeAll('loading'); //关闭加载层
			},"","post");
		}
		var initStatistics = function(data){
			$("#order_all_count").html(data.allCount);
			$("#order_serch_count").html(data.totalCount);
			$("#order_percent").html(data.percent);
			
			$("#order_all_money_count").html(data.all_amount_money);
			$("#order_serch_amount_money").html(data.amount_money);
			$("#money_percent").html(data.money_percent);
		}
		var orderlist_tpl;//订单列表模板
		var order_price_tpl;//修改价格接口
		var initTpl = function(){
			orderlist_tpl = arttemplateUtil.template.compile($.setHTML('order/templates/orderlist_tpl.html'));
			order_price_tpl = arttemplateUtil.template.compile($.setHTML('order/templates/order_price_tpl.html'));
		}
		//修改价格
		$(document).off('click','.editPrice').on('click','.editPrice',function(){
			$('#priceModal').modal('show');
			$("#price_tpl").html(order_price_tpl({
				of_id:$(this).attr("data-of-id"),
				user_account:$(this).attr("data-user-account"),
				order_price:$(this).attr("data-total"),
				goods_price:$(this).attr("data-goods-price"),
				ship_price:$(this).attr("data-ship-price")
			}));
			$("#priceForm input[name=goods_price]").keyup(function(){
				var order_price = (parseFloat($("#priceForm input[name=goods_price]").val())+
						parseFloat($("#priceForm input[name=ship_price]").val())).toFixed(2);
				if (isNaN(order_price)){
					$("#all_price").html(0);
				}else{
					$("#all_price").html(order_price);
				}
			  
			});
			$("#priceForm input[name=ship_price]").keyup(function(){
				  $("#all_price").html((parseFloat($("#priceForm input[name=goods_price]").val())+
					parseFloat($("#priceForm input[name=ship_price]").val())).toFixed(2));
			});
		});
		
		//修改价格保存
		$(document).off('click','.priceSubmit').on('click','.priceSubmit',function(){
			var validaddrForm = $("#priceForm").Validform({
				tiptype:4,
				postonce:true,
				isShowRight:false,
				datatype:{"priceCompare":function(gets,obj,curform,regxp){
					var reg = /(^[+]?[1-9]\d{0,7}(\.\d{1,2})?$)|(^[+]?[0]{1}(\.\d{1,2})?$)/;
					var text ="商品总价格";
					var maxnum = 0;
					if($(obj).attr("id")=="ship_price"){
						maxnum = $.trim($("#priceForm input[name=max_ship_price]").val());
						text = "配送金额"
					}else{
						maxnum = $.trim($("#priceForm input[name=max_goods_price]").val());
					}
					if($(obj).val() !=0 && !reg.test($(obj).val())) {
						return text+"式不正确[0-99999999.99]";
					}
					
					$("#priceForm input[name="+$(obj).attr("id")+"]").removeClass("Validform_error");
					$(obj).next().remove();
					return true;
				}}
			});
			if(!validaddrForm.check()){
				return false;
			}
			if(parseFloat($("#all_price").html())==parseFloat($("#priceForm input[name=order_price]").val())){
				window.layer.msg("订单价格修改成功");
				$('#priceModal').modal('hide');
			}else{
				$(this).attr("disabled","disabled");
				common.common_ajax_request('/api/seller/v1/updateOrderPrice',$('#priceForm').serializeArray(),false,function(data,code,msg){
					window.layer.msg(msg.businessNote);
					if(msg.businessCode==0){
						$('#priceModal').modal('hide');
						initData();
					}
				},"","get",function(){
					$(".priceSubmit").removeAttr("disabled");
				});
			}
		});
		
		//订单列表
		var setData = function(data){
			var res = {
					order_tab:param.order_tab,
					records:data.records,
					url:config.imgRoot()
			}
			$("#orderlist").html(orderlist_tpl(res));
		}
		//分页回调
		var callback = function(page){
			param.pageNo=page
			initData();
		}
		//初始化
		var init = function() {
			$.each($('#serchForm').serializeArray(), function(i, item) {
				if(item.value==""){
					delete param[item.name]
				}else{
					param[item.name]=item.value;
				}
			});
			initTpl();
			initData();
			arttemplateUtil.auload();
			autoComplete();
		};
		//查询验证
		var validserchform = $("#serchForm").Validform({
			tiptype:4,
			postonce:true,
			isShowRight:false
		});
		
		//页签选择,更换页签需要清空查询条件
		$('#tabs-632335 a').click(function (e) {
			  param.order_tab=$(this).attr("tab_value");
			  param.pageNo=1;
			  initData();
		});
		
	  //查询
	  $('.serchBtn').on('click',function(){
		  if(validserchform.check()){
			param.pageNo=1;
			delete param["isNew"];
			$.each($('#serchForm').serializeArray(), function(i, item) {
				if(item.value==""){
					delete param[item.name]
				}else{
					param[item.name]=item.value;
				}
			});
			initData();
	  	  }
	   });
	   //重置
		$('.resetBtn').on('click',function(){
			$.each($('#serchForm').serializeArray(), function(i, item) {
				delete param[item.name];
			});
			delete param["isNew"];
			//$("#serchForm select[name=goods_type]").val("");
			$('#serchForm')[0].reset();
			//$("form[name=serchForm] input").val("");
			$("#serchForm input").removeClass("Validform_error");
			$("#serchForm .Validform_checktip").removeClass("Validform_wrong").html("");
			//$("#serchForm input[name=isNew]").prop("checked",false);
			//$("#serchForm input[name=isNew]").val("1");
			initData();
		});
		//导出
		$('.excelBtn').on('click',function(){
			$("#excel").attr("action",config.apiRoot()+"/api/seller/v1/exportOrderList");
			var html = [];
			html.push("<input name=\"order_tab\" type='hidden' id=\"order_tab\" value=\""+param.order_tab+"\" />");
			$.each($('#serchForm').serializeArray(), function(i, item) {
				html.push("<input name=\""+item.name+"\" type='hidden' id=\""+item.name+"\" value=\""+item.value+"\" />");
			});
			$.each(config.setFullParameterArr({}), function(i, item) {
				html.push("<input name=\""+i+"\" type='hidden' id="+i+" value=\""+item+"\" />");
			});
			$("#excel").html(html.join(""));
			$("#excel").submit();
		});
		//消息提示
		$(document).off('click','.mesInfo').on('click','.mesInfo',function(event){
			//取消事件冒泡  
		    event.stopPropagation();  
		    msg_util.msgDialog(this,function(){
		    	initData();
		    });
		});
		
		//搜索自动补全;给搜索框注册自动联想完成事件
		var autoComplete = function(){
				jQuery('#goods_name').typeahead({
					source: function (query, process) {
						//query是输入值
						if(query !== 'undefined' && typeof query != 'undefined' && query !==""){
							common.ajax_request2('post','/api/seller/v1/queryGoodsListByGoodsName', { "goods_name": query,goods_label:$("#goods_type").val()}, function ($data,$code) {
								if($code==200){
									process($data);
								}
								
							});
						}else{
							process({});
						}
					},
					matcher:function(item){
						return true;
					},
					updater: function (item) {
						return item; //这里一定要return，否则选中不显示
					},
					afterSelect: function (item) {
						//选择项之后的时间，item是当前选中的项
						//alert(item);
					},
					items: 10, //显示8条
					delay: 500, //延迟时间
					autoSelect:true
				});
		};
		
		init();
	});
});
